#!/usr/bin/env python3
# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "pyyaml>=6",
# ]
# ///

"""
Set environment variables required for the CI jobs by inspection of the
clouds.yaml file. This is useful where you only have this file.

To set variables:

    $ eval $(./script/getenvvar)

To unset them:

    $ unset $(compgen -v | grep OS_)
"""

import argparse
import os
from pathlib import Path
import sys

import yaml

parser = argparse.ArgumentParser()
parser.add_argument(
    'cloud',
    default=os.getenv('OS_CLOUD'),
    nargs='?',
    help="Cloud to export credentials for",
)

args = parser.parse_args()

for p in (
    Path('clouds.yaml'),
    Path('~/.config/openstack/clouds.yaml').expanduser(),
    Path('/etc/openstack/clouds.yaml'),
):
    if not p.exists():
        continue

    with p.open() as fh:
        data = yaml.safe_load(fh)
        break
else:
    print('Could not find clouds.yaml file', file=sys.stderr)
    sys.exit(1)

if not args.cloud:
    print('Need to provide cloud argument or set OS_CLOUD', file=sys.stderr)
    sys.exit(1)

if args.cloud not in data.get('clouds', {}) or {}:
    print(f'Could not find cloud {args.cloud} in {str(p)}', file=sys.stderr)
    sys.exit(1)

cloud = data['clouds'][args.cloud]

if 'auth' not in cloud:
    print(f'Missing auth section for cloud {cloud}', file=sys.stderr)
    sys.exit(1)

auth = cloud['auth']

if 'username' not in auth or 'password' not in auth:
    print('Only password authentication supported', file=sys.stderr)
    sys.exit(1)

# FIXME: This should work but does not, since the check for auth credentials
# is just 'OS_USERNAME == admin'

# user_id = auth.get('user_id')
# project_id = auth.get('project_id')
# if not user_id or not project_id:
#     import openstack
#     conn = openstack.connect(args.cloud)
#     auth_ref = conn.config.get_auth().get_auth_ref(conn.session)
#
#     if not user_id:
#         user_id = auth_ref.user_id
#
#     if not project_id:
#         project_id = auth_ref.project_id
#
# result = f"""
# unset OS_CLOUD
# export OS_AUTH_URL={auth['auth_url']}
# export OS_USERID={user_id}
# export OS_PASSWORD={auth['password']}
# export OS_PROJECT_ID={project_id}
# export OS_REGION_NAME={cloud['region_name']}
# """.strip()

result = f"""
unset OS_CLOUD;
export OS_AUTH_URL={auth['auth_url']};
export OS_USERNAME={auth['username']};
export OS_PASSWORD={auth['password']};
export OS_PROJECT_NAME={auth['project_name']};
export OS_DOMAIN_ID={auth['user_domain_id']};
export OS_REGION_NAME={cloud['region_name']};
"""

print(result.strip())
